package com.zp.business.controller.lab;

import com.zp.business.entity.lab.bo.LabApplyBo;
import com.zp.business.entity.lab.bo.SaveParamBo;
import com.zp.business.entity.lab.vo.LabApplyVo;
import com.zp.business.service.lab.ILabApplyService;
import com.zp.common.core.controller.BaseController;
import com.zp.common.core.entity.R;
import com.zp.common.core.validate.AddGroup;
import com.zp.common.core.validate.EditGroup;
import com.zp.common.excel.utils.ExcelUtil;
import com.zp.common.log.annotation.Log;
import com.zp.common.log.enums.BusinessType;
import com.zp.common.myBatis.core.page.PageQuery;
import com.zp.common.myBatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 检验申请主控制器
 * 前端访问路由地址为:/business/labApply
 *
 * @author zhang peng
 * @ date 2024-03-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/labApply")
public class LabApplyController extends BaseController {

    private final ILabApplyService iLabApplyService;

    /**
     * 查询检验申请主列表 (分页)
     */
//    @SaCheckPermission("business:labApply:list")
    @GetMapping("/selectPage")
    public TableDataInfo<LabApplyVo> selectPage(LabApplyBo bo, PageQuery pageQuery) {
        return iLabApplyService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询检验申请主列表 (不分页)
     */
//    @SaCheckPermission("business:labApply:list")
    @GetMapping("/list")
    public R<List<LabApplyVo>> list(LabApplyBo bo) {
        List<LabApplyVo> list = iLabApplyService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出检验申请主列表
     */
//    @SaCheckPermission("business:labApply:export")
    @Log(title = "检验申请主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(LabApplyBo bo, HttpServletResponse response) {
        List<LabApplyVo> list = iLabApplyService.queryList(bo);
        ExcelUtil.exportExcel(list, "检验申请主", LabApplyVo.class, response);
    }

    /**
     * 获取检验申请主详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("business:labApply:list")
    @GetMapping("/{id}")
    public R<LabApplyVo> getInfo(@NotNull(message = "主键不能为空") @PathVariable String id) {
        return R.ok(iLabApplyService.queryById(id));
    }

    /**
     * 新增检验申请主
     */
//    @SaCheckPermission("business:labApply:add")
    @Log(title = "检验申请主", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody LabApplyBo bo) {
        return toAjax(iLabApplyService.insertByBo(bo) ? 1 : 0);
    }

    /**
     * 修改检验申请主
     */
//    @SaCheckPermission("business:labApply:edit")
    @Log(title = "检验申请主", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody LabApplyBo bo) {
        return toAjax(iLabApplyService.updateByBo(bo) ? 1 : 0);
    }

    /**
     * 删除检验申请主
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("business:labApply:remove")
    @Log(title = "检验申请主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Integer> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] ids) {
        int i = iLabApplyService.deleteWithValidByIds(Arrays.asList(ids), true);
        return R.ok(i);
    }

    /**
     * 保存检验申请明显
     */
    @Log(title = "保存检验申请明显", businessType = BusinessType.INSERT)
    @PostMapping("saveLabApplyAndItem")
    public R<Void> saveLabApplyAndItem(@Validated(AddGroup.class) @RequestBody SaveParamBo bo) {
        return toAjax(iLabApplyService.saveLabApplyAndItem(bo) ? 1 : 0);
    }

    /**
     * 修改收费状态
     *
     * @param id 主键
     */
//    @SaCheckPermission("business:examApply:remove")
    @Log(title = "检查申请主", businessType = BusinessType.UPDATE)
    @GetMapping("/refundApplication/{id}/{chargeFlag}")
    public R<Boolean> refundApplication(@NotEmpty(message = "主键不能为空") @PathVariable String id,
                                        @NotEmpty(message = "收费类型不能为空") @PathVariable String chargeFlag) {
        return R.ok(iLabApplyService.refundApplication(id, chargeFlag));
    }


    /**
     * 修改检验收费状态、确认状态
     */
    @Log(title = "修改检验收费状态、确认状态", businessType = BusinessType.UPDATE)
    @PostMapping("labConfirmOk")
    public R<Void> examConfirmOk(@RequestBody LabApplyBo bo) {
        return toAjax(iLabApplyService.labConfirmOk(bo) ? 1 : 0);
    }
}
